package com.csjbot.snowbot_rogue.servers.slams.agent.Util;

import android.os.Environment;

import com.csjbot.snowbot_rogue.servers.slams.events.MapOperationEvent;
import com.slamtec.slamware.AbstractSlamwarePlatform;
import com.slamtec.slamware.geometry.Line;
import com.slamtec.slamware.robot.ArtifactUsage;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenqi on 2017/2/27.
 */

public class FileUtil {
    private static String path = Environment.getExternalStorageDirectory()+ "/map.map";

    /**
     * 存在本地的地图
     * @param map
     */
    public static void saveMapToFile(MapBean map){
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        File f = new File(FileUtil.path);
        try {
            fos = new FileOutputStream(f);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(map);    //括号内参数为要保存java对象
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                oos.close();
                fos.close();
                EventBus.getDefault().post(new MapOperationEvent(MapOperationEvent.SAVEMAPOK));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 从本地文件读取
     * @return
     */
    public static Object read() {
        ObjectInputStream ois=null;
        Object people=null;
        try {
            //获取输入流
            ois=new ObjectInputStream(new FileInputStream(new File(path)));
            //获取文件中的数据
            people = ois.readObject();
            return people;
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if (ois!=null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return people;
    }

    /**
     * get track
     * @param map
     * @return
     */
    public static List<Line> getTrack(MapBean map)
    {
        List<Line> tracks = new ArrayList<>();
        for (int i = 0; i < map.getLines().size() ; i++) {
            Line track = new Line(map.getLines().get(i).getSegmentId(),
                    map.getLines().get(i).getLine_startPoint_x(),
                    map.getLines().get(i).getLine_startPoint_y(),
                    map.getLines().get(i).getLine_endPoint_x(),
                    map.getLines().get(i).getLine_endPoint_y());
            tracks.add(track);
        }
        return tracks;
    }

    /**
     * get walls
     * @param map
     * @return
     */
    public static List<Line> getWall(MapBean map)
    {
        List<Line> walls = new ArrayList<>();
        for (int i = 0; i < map.getWalls().size() ; i++) {
            Line track = new Line(map.getWalls().get(i).getSegmentId(),
                    map.getWalls().get(i).getLine_startPoint_x(),
                    map.getWalls().get(i).getLine_startPoint_y(),
                    map.getWalls().get(i).getLine_endPoint_x(),
                    map.getWalls().get(i).getLine_endPoint_y());
            walls.add(track);
        }
        return walls;
    }

    /**
     * save wall
     * @param robotPlatform
     * @return
     */
    public static List<MapWalls> SaveWalls(AbstractSlamwarePlatform robotPlatform)
    {

        List<Line>  walls = robotPlatform.getLines(ArtifactUsage.ArtifactUsageVirutalWall);
        List<MapWalls> mapWalls = new ArrayList<>();
        //实例化当前需要存储的数据
        for (int w = 0; w < walls.size(); w++) {
            MapWalls mapWall = new MapWalls(walls.get(w).getSegmentId()
                    ,walls.get(w).getStartX()
                    ,walls.get(w).getStartY()
                    ,walls.get(w).getEndX()
                    ,walls.get(w).getEndY());
            mapWalls.add(mapWall);
        }
       return mapWalls;
    }

    /**
     * save track
     * @param robotPlatform
     * @return
     */
    public static List<MapLines> SaveTrack(AbstractSlamwarePlatform robotPlatform)
    {
        List<Line> line = robotPlatform.getLines(ArtifactUsage.ArtifactUsageVirtualTrack);
        List<MapLines> maplines = new ArrayList<>();
        for (int i = 0; i < line.size() ; i++) {
            MapLines mapline = new MapLines(line.get(i).getSegmentId(),
                    line.get(i).getStartX(),
                    line.get(i).getStartY(),
                    line.get(i).getEndX(),
                    line.get(i).getEndY());
            maplines.add(mapline);
        }
        return maplines;
    }
}
